<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Send email
 *
 * Usage: php index.php tools/cron_email send 
 */

/*
 * $Id: cron_email.php 163 2011-10-14 15:20:02Z leikou@163.com $
 */

class Cron_email extends CI_Controller
{
    const SEND_LIMIT = 100;


    public function send()
    {
        // Get file lock
        $lock_file = $this->config->item('filelock_path') . '/cron_email';
        $this->load->library('File_lock');
        $this->file_lock->init($lock_file);
        $lock = $this->file_lock->lock();
        if( ! $lock)
        {
            return;
        }

        $this->real_send();

        $this->file_lock->unlock();
    }

    public function real_send()
    {
        // Get email_list
        $email_list = $this->db
            ->select('ea.id, title, message, address')
            ->from('email_address AS ea')
            ->join('email AS e', 'ea.email_id = e.id')
            ->where('ea.status', OA_CONST_MESSAGE::STATUS_INIT)
            ->where('e.status', OA_CONST_MESSAGE::STATUS_INIT)
            ->limit(self::SEND_LIMIT, 0)
            ->get()
            ->result_array();

        $this->load->library('email');

        // Set email_sender
        $this->config->load('email', TRUE);
        $this->email_sender = $this->config->item('smtp_user', 'email');

        foreach($email_list as $email)
        {
            $this->db->trans_start();

            $sql = "SELECT id, status FROM email_address".
                " WHERE id=? FOR UPDATE";
            $data = array($email['id']);
            $locked = $this->db->query($sql, $data)->first_row('array');

            if($locked['status'] != OA_CONST_MESSAGE::STATUS_INIT)
            {
                continue;
            }

            $data = array(
                'status' => OA_CONST_MESSAGE::STATUS_DONE,
                'send_dateline' => time(),
            );
            $this->db->where('id', $email['id'])->update('email_address', $data);

            //--------------- Send email ------------------
            $this->email->clear();
            $this->email->from($this->email_sender);
            $this->email->to($email['address']);
            $this->email->subject($email['title']);
            $this->email->message($email['message']);

            if( ! $this->email->send()) {
                $this->db->trans_rollback();
                continue;
            }

            $this->db->trans_complete();
        }

        // If all email_address be processed, update email.status
        $sql = "UPDATE email e ".
            " LEFT JOIN email_address ea ON (e.id=ea.email_id AND ea.status <> ?)".
            " SET e.status=?".
            " WHERE e.status=? AND ea.email_id IS NULL";
        $data = array(
            OA_CONST_MESSAGE::STATUS_DONE,
            OA_CONST_MESSAGE::STATUS_DONE,
            OA_CONST_MESSAGE::STATUS_INIT,
        );
        $this->db->query($sql, $data);
    }
}

/* End of file: cron_email.php */
/* Location ./application/tools/cron_email.php */
